<template>
  <a-modal
    :title="title"
    :width="modalWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    :destroyOnClose="destoryClose"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-card :bordered="false">
      <!-- 操作按钮区域 -->
      <!-- table区域-begin -->
      <div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{selectedRowKeys.length }}</a>项
        </div>

        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"

          @change="handleTableChange"
          :customRow="rowFun"
        >
          <template slot="isQuality" slot-scope="text">
            {{ text ? '是' : '否' }}
          </template>
          <template slot="currentArrivalNum" slot-scope="text, record">
            <editable-cell :text="text" @change="onCellChange(record, 'currentArrivalNum', $event)" />
          </template>
        </a-table>
      </div>
    </a-card>
  </a-modal>
</template>

<script>
  import { JeecgListMixin } from '@lone/lone-vue/src/mixins/JeecgListMixin'
  import { getAction} from '@lone/lone-vue/src/api/manage'
  import EditableCell from './EditableCell'

  export default {
    name: "DeliveryOrderListModal",
    mixins: [JeecgListMixin],
    components: {
      EditableCell
    },
    data () {
      return {
        title:"到货单选择",
        dataSource:[],
        destoryClose:true,
        num:'',
        //下标
        index:-1,
        visible: false,
        modalWidth:800,
        confirmLoading: false,
        rowSelectType:'checkbox',
        description: '到货单信息管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'物资编码',
            align:"center",
            dataIndex: 'materialCode'
          },
          {
            title:'物资名称',
              align:"center",
            dataIndex: 'materialName'
          },
          {
            title:'规格',
              align:"center",
            dataIndex: 'specs'
          },
          {
            title:'型号',
              align:"center",
            dataIndex: 'model'
          },
          {
            title:'计量单位',
              align:"center",
            dataIndex: 'unit'
          },
          {
            title: '是否需要质检',
            dataIndex: 'isQuality',
            scopedSlots: { customRender: 'isQuality' },
          },
          {
            title:'未到货数量',
              align:"center",
            dataIndex: 'noArrivalNum'
          },
          {
            title:'本次到货数量',
            align:"center",
            dataIndex: 'currentArrivalNum',
            scopedSlots: { customRender: 'currentArrivalNum' },
          }],
        url: {
          list: '/cg/mmsWzPurchaseOrder/arrivedList'
        }
      }
    },
    created () {

    },
    methods: {
      onCellChange(record, dataIndex, value) {
        let currentArrivalNum = record.currentArrivalNum;
        const dataSource = [...this.dataSource];
        const target = dataSource.find(item => item.currentArrivalNum === currentArrivalNum);
        if (target) {
          target[dataIndex] = value;
          dataSource[dataSource.indexOf(target)] = target;
          this.dataSource = dataSource;
        }
      },
      showDeliveryOrderListInfo(orderCode, num,selectedRowKeys){
        this.num = num
        if(selectedRowKeys  !=''){
          this.selectedRowKeys = []
          this.selectedRowKeys.push(selectedRowKeys)
        }
        this.queryParam.orderId = orderCode;
        this.loadData(1);
        this.visible = true
      },
      handleOk(){
        let selection = [];
        for(let f in this.dataSource){
          let item = this.dataSource[f], currentArrivalNum = parseInt(item['currentArrivalNum'] || 0);
          if(currentArrivalNum > 0){
            selection.push(item);
          }
        }
        if(this.index>=0){
          this.$emit("ok",selection,this.index);
        }else{
          this.$emit("ok",selection, this.num);
        }
        this.handleCancel();
      },
      handleCancel () {
        this.close()
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.index = 0
      },
      rowFun (record, index) {
        return {
          on: {
            dblclick: () => {
              this.$emit("ok",record,this.index)
              this.close()
            }
          }
        }
      }
    }
  }
</script>

<style lang="less" scoped>

</style>